/**
 * @(#) CongMoneyMediator.as
 *
 * @date		2009. 03. 25.
 * @version		1.0
 * @author		baek, kwang hyoun
 * @description	건강보험증 신청 - Mediator
 * 				건강보험증을 조회하고 신청하는 Presentation Logic을 관리
 */
package com.skt.common_hr.babybt.view
{
	import com.skt.common_hr.approval_sac.view.components.ApprovalView;
	import com.skt.common_hr.babybt.model.BabybtApprovalProxy;
	import com.skt.common_hr.babybt.model.BabybtApprovalProxy2;
	import com.skt.common_hr.babybt.model.BabybtApprovalProxy3;
	import com.skt.common_hr.babybt.model.BabybtApprovalProxy4;
	import com.skt.common_hr.babybt.view.components.BabybtOverView;
	import com.skt.common_hr.common.approval.RequestApprovalMediator;
	import com.skt.common_hr.common.puremvc.OverViewMediator;
	import com.wcl.utils.DateUtil;
	
	import flash.events.Event;
	
	import mx.collections.ArrayCollection;
	
	import org.puremvc.as3.multicore.interfaces.INotification;

	public class BabybtMediator extends OverViewMediator
	{
		public static const NAME:String = "BabybtMediator";


		/**
		* 생성자
		*
		* @param
		* @return void
		*/
        public function BabybtMediator( viewComponent:Object )
        {
            super( NAME, viewComponent );
        }

		/**
		* View를 초기화 한다.
		*	- View에 기본사항을 출력하고 data를 load
		* @param
		* @return void
		* 최초 한번만 불림
		*/
 		override protected function initializeView():void
 		{
			super.initializeView();
 		}
		/***
		 * 활성화가 될때마다 불림.
		 */
		override protected function activatedView():void
		{
			super.activatedView();
			view.clearView();
			
			view.listView_imsin.BEGDA = DateUtil.firstDateofThisYear();
			view.listView_imsin.ENDDA = new Date();
			
			view.listView_imsan.BEGDA = DateUtil.firstDateofThisYear();
			view.listView_imsan.ENDDA = new Date();
			
			view.listView_wts.BEGDA = DateUtil.firstDateofThisYear();
			view.listView_wts.ENDDA = new Date();						
			
			view.listView_ot.BEGDA = DateUtil.firstDateofThisYear();
			view.listView_ot.ENDDA = new Date();			
 			loadList();
			setEventListener();
			view.treeMenu.selectedIndex = 0;
		}

		override protected function closedView():void
		{
			removeEventListener();
		}

 		private function setEventListener():void
 		{
 			view.addEventListener("APPROVAL", execApproval);
 			view.addEventListener("APPROVAL2", execApproval2);
 			view.addEventListener("APPROVAL3", execApproval3);
 			view.addEventListener("APPROVAL4", execApproval4);
 			view.addEventListener(ApprovalView.CLOSE_VIEW, onCloseView);
 			
			//view.addEventListener(noticeOverview.NoticeWrite, onNoticeWrite);
			//view.addEventListener(NoticeEvent.SELECTED_ITEM, onDetailView); 			
 		}
 		/* 
 		private function onNoticeWrite(event:Event):void
 		{
			view.vsMain.selectedIndex = 1;
 			facade.sendNotification(BabybtFacade.MODE_WRITE_NOTICE, this);
 		}
 		 */
 		private function onCloseView(e:Event):void
		{
			trace(111);
			//			view.closeView();
//			bActivated = false;
			//facade.sendNotification(CANCEL_APPROVAL_VIEW, this);
		}

		/* 임신부 신청	 */
		private function execApproval(event:Event):void
		{
 			var approval:RequestApprovalMediator = facade.retrieveMediator(RequestApprovalMediator.NAME) as RequestApprovalMediator;
 			if(approval==null) {
 				approval = new RequestApprovalMediator(view, "17");
 				facade.registerMediator(approval);
 			}
 			 			
 			var appProxy:BabybtApprovalProxy = facade.retrieveProxy(BabybtApprovalProxy.NAME) as BabybtApprovalProxy;
 			appProxy.key = view.listView_imsin.sSave_gwarekey;
	 			 			
 			var BabybtObj:Object = {};
 			BabybtObj.PERNR 	= view.listView_imsin.oSaveObject.PERNR;
 			BabybtObj.BEGDA 	= view.listView_imsin.oSaveObject.BEGDA_TX; 	
 			BabybtObj.ENDDA 	= view.listView_imsin.oSaveObject.ENDDA_TX;
 			
 			BabybtObj.ORGTX 	= view.listView_imsin.sSave_orgtx;
 			BabybtObj.ENAME 	= view.listView_imsin.sSave_ename;
 			BabybtObj.WEEK		= view.listView_imsin.sSave_week;
 			appProxy.BabybtObj= BabybtObj;				
			approval.execRequest(view, appProxy, false); // bTRPAYC : 정산포함여부
		}
		
			
			
		/* 임산부 신청 */
		private function execApproval2(event:Event):void
		{
 			var approval:RequestApprovalMediator = facade.retrieveMediator(RequestApprovalMediator.NAME) as RequestApprovalMediator;
 			if(approval==null) {
 				approval = new RequestApprovalMediator(view, "18");
 				facade.registerMediator(approval);
 			}
 			 			
 			var appProxy:BabybtApprovalProxy2 = facade.retrieveProxy(BabybtApprovalProxy2.NAME) as BabybtApprovalProxy2;
 			appProxy.key = view.listView_imsan.sSave_gwarekey;
	 			 			
 			var BabybtObj:Object = {};
 			BabybtObj.PERNR 	= view.listView_imsan.oSaveObject.PERNR;
 			BabybtObj.ENDDA 	= view.listView_imsan.oSaveObject.ENDDA_TX;
 			BabybtObj.ORGTX 	= view.listView_imsan.sSave_orgtx; 
 			BabybtObj.ENAME 	= view.listView_imsan.sSave_ename;
 			
 			appProxy.BabybtObj= BabybtObj				
			approval.execRequest(view, appProxy, false); // bTRPAYC : 정산포함여부
		}
		
		/* 근로시간 단축신청 */
		private function execApproval3(event:Event):void
		{
 			var approval:RequestApprovalMediator = facade.retrieveMediator(RequestApprovalMediator.NAME) as RequestApprovalMediator;
 			if(approval==null) {
 				approval = new RequestApprovalMediator(view, "15");
 				facade.registerMediator(approval);
 			}
 			 			
 			var appProxy:BabybtApprovalProxy3 = facade.retrieveProxy(BabybtApprovalProxy3.NAME) as BabybtApprovalProxy3;
 			appProxy.key = view.listView_wts.sSave_gwarekey;
	 			 			
 			var BabybtObj:Object = {};
 			BabybtObj.PERNR 	= view.listView_wts.oSaveObject2.PERNR;
 			
 			
 			BabybtObj.BEGDA 	= view.listView_wts.oSaveObject2.BEGDA_TX; 	
 			BabybtObj.ENDDA 	= view.listView_wts.oSaveObject2.ENDDA_TX;
 			
 			BabybtObj.BEGUZ 	= view.listView_wts.oSaveObject2.BEGUZ_TX; 	
 			BabybtObj.ENDUZ 	= view.listView_wts.oSaveObject2.ENDUZ_TX;
 			 			
 			BabybtObj.ORGTX 	= view.listView_wts.sSave_orgtx;
 			BabybtObj.ENAME 	= view.listView_wts.sSave_ename;
 			
 			BabybtObj.SEASN 	= view.listView_wts.oSaveObject2.SEASN;
 			
 			appProxy.BabybtObj= BabybtObj;				
			approval.execRequest(view, appProxy, false); // bTRPAYC : 정산포함여부
		}		


		
		/* OT 신청 */
		private function execApproval4(event:Event):void
		{
 			var approval:RequestApprovalMediator = facade.retrieveMediator(RequestApprovalMediator.NAME) as RequestApprovalMediator;
 			if(approval==null) {
 				approval = new RequestApprovalMediator(view, "16");
 				facade.registerMediator(approval);
 			}
 			 			
 			var appProxy:BabybtApprovalProxy4 = facade.retrieveProxy(BabybtApprovalProxy4.NAME) as BabybtApprovalProxy4;
 			appProxy.key = view.listView_ot.sSave_gwarekey;
	 			 			
 			var BabybtObj:Object = {};
 			BabybtObj.PERNR 	= view.listView_ot.oSaveObject.PERNR;
 			
 			BabybtObj.BEGDA_DATE 	= view.listView_ot.oSaveObject.BEGDA_DATE; 	
 			BabybtObj.ENDDA_DATE 	= view.listView_ot.oSaveObject.ENDDA_DATE;
 			BabybtObj.BIRDD_DATE 	= view.listView_ot.oSaveObject.BIRDD_DATE;
 			
 			BabybtObj.CYES1 	= view.listView_ot.oSaveObject.CYES1;
 			BabybtObj.CYES2 	= view.listView_ot.oSaveObject.CYES2;
 			BabybtObj.CYES3 	= view.listView_ot.oSaveObject.CYES3;
 			
 			BabybtObj.CYES1N 	= view.listView_ot.oSaveObject.CYES1N;
 			BabybtObj.CYES1H 	= view.listView_ot.oSaveObject.CYES1H;
 			BabybtObj.CYES2N 	= view.listView_ot.oSaveObject.CYES2N;
 			BabybtObj.CYES2H 	= view.listView_ot.oSaveObject.CYES2H; 			
 			
 			
 			
 			BabybtObj.O_MON 	= view.listView_ot.O_MON;
 			BabybtObj.O_WEEK 	= view.listView_ot.O_WEEK;
 			
// 			BabybtObj.ORGTX 	= view.listView_ot.sSave_orgtx;
// 			BabybtObj.ENAME 	= view.listView_ot.sSave_ename;
 			
 			//BabybtObj.SEASN 	= view.listView_ot.oSaveObject.SEASN;
 			
 			appProxy.BabybtObj= BabybtObj;				
			approval.execRequest(view, appProxy, false); // bTRPAYC : 정산포함여부
		}	
		
								
 		private function removeEventListener():void
 		{
			//view.removeEventListener(HthIcFacade.HthIcWrite, onClickNew);
 			view.removeEventListener("APPROVAL", execApproval);
 			view.removeEventListener("APPROVAL2", execApproval2);
 			view.removeEventListener("APPROVAL3", execApproval3);
 			view.removeEventListener("APPROVAL4", execApproval4);
 			view.removeEventListener(ApprovalView.CLOSE_VIEW, onCloseView);			
 		}


 		private function loadList():void
 		{
 			view.listView_imsin.loadMainList();
// 			var mProxy:MedicalMoneyProxy = facade.retrieveProxy(MedicalMoneyProxy.NAME) as MedicalMoneyProxy;
//			mProxy.loadList(Variables.myPernr);
 		}


		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Process MVC Messages
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		* 본 Class에서 처리해야할 Noti들을 등록한다.
		*
		* @param
		* @return void
		*/
		override public function listNotificationInterests():Array
		{
			return basicInterestMessages.concat([
					RequestApprovalMediator.APPROVAL_CANCEL,
					RequestApprovalMediator.APPROVAL_COMPLETE					
					// reload
//					ReqMedicalMoneyMediator.CLOSED_REQUEST_VIEW,
//					MedicalMoneyProxy.LOAD_MEDICALMONEY_LIST_COMPLETE,
//					MedicalMoneyProxy.LOAD_MEDICALMONEY_LIST_FAILED
					]);
		}


		/**
		* Noti별로 처리한다.
		*
		* @param
		* @return void
		*/
		override public function handleNotification(note:INotification):void
		{
			super.handleNotification(note);
			switch(note.getName())
			{
				// 신청완료시...
				case RequestApprovalMediator.APPROVAL_COMPLETE:					// 결재 요청  성공
					//임신부 신청
					if(view.treeMenu.selectedItem.@data == 2) {
						view.listView_imsin.loadMainList();
						view.listView_imsin.oSaveObject = {};
					}
					// 임산부 신청
					else if(view.treeMenu.selectedItem.@data == 3) {
						view.listView_imsan.loadMainList();
						view.listView_imsan.oSaveObject = {};
					}
					// 단축 근로 신청
					else if(view.treeMenu.selectedItem.@data == 4) {
						view.listView_wts.loadMainList2();
						view.listView_wts.oSaveObject2 = {};
					}
					// ot신청
					else if(view.treeMenu.selectedItem.@data == 5) {
						view.listView_ot.loadMainList();
						view.listView_ot.oSaveObject = {};
					}		
					break;
				// 신청 취소시..
				case RequestApprovalMediator.APPROVAL_CANCEL:					// 결재 요청 취소
					if(view.treeMenu.selectedItem.@data == 2) {
						view.listView_imsin.onClickDel_forCancel(view.listView_imsin.sSave_gwarekey);
					}
					else if(view.treeMenu.selectedItem.@data == 3) {
						// imasan으로 콜함
						view.listView_imsan.onClickDel_forCancel(view.listView_imsan.sSave_gwarekey);
					}
					else if(view.treeMenu.selectedItem.@data == 4) {
						// wts으로 콜함
						view.listView_wts.onClickDel_forCancel2(view.listView_wts.sSave_gwarekey);
					}
					else if(view.treeMenu.selectedItem.@data == 5) {
						// ot로 콜함
						view.listView_ot.onClickDel_forCancel(view.listView_ot.sSave_gwarekey);
					}												
					break;
					
					
														
				// 의료비 리스트  조회 Proxy 호출결과 처리
//				case MedicalMoneyProxy.LOAD_MEDICALMONEY_LIST_COMPLETE :
//					setListView(MedicalMoneyProxy(note.getBody()).mainList);
//					break;
				default:
					//trace(this.instanceID+"에서 처리 안된 Noti가 검출됨 : " + note.getBody());
					break;
			}
		}

 		/**
		 * Proxy에서 DataLoad를 완료한 후 호출됨
		 *     - load된 데이터를 화면에 출력
		 * @param
		 * @return void
		 */
 		private function setListView(list:ArrayCollection):void
 		{
// 			view.medicalMoneyDataList = list;
// 			commonFacade.setStatusMessage("STATUS_LOADED_LIST", [list.length.toString()]);
// 			if(list.length == 0) {
// 				view.showInitialTip();
// 			}
 		}


		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event handler (from View, Internal or....)
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
       private function get view():BabybtOverView
        {
            return viewComponent as BabybtOverView;
        }


	}
}